package myky.com.doctorapp.utils;

import android.content.Context;

import com.securepreferences.SecurePreferences;

import java.util.Set;

/**
 * Description: 加密的SharePrefence
 *
 * @author xiangyao
 * Created by 2019/4/3 4:14 PM
 */
public class SecretSharePrefence {

    private static SecurePreferences sharedPreferences;

    private final static int DEFAULTINT = -1;

    private final static String DEFAULTSTRING = "";

    private final static boolean DEFAULTBOOLEAN = false;

    private final static float DEFAULTFLOAT = 0f;

    private final static long DEFAULTLONG = 0L;


    private SecretSharePrefence() {


    }

    public static void initSecreSharePrefence(Context context) {


        sharedPreferences = getSharedPreferences(context);
    }


    private synchronized static SecurePreferences getSharedPreferences(Context context) {

        if (sharedPreferences == null) {
            synchronized (SecretSharePrefence.class) {
                if (sharedPreferences == null) {

                    sharedPreferences = new SecurePreferences(context, "userpassword", "my_user_prefs.xml");

                }
            }
        }
        return sharedPreferences;
    }

    public static boolean putIntValue(String key, int value) {

        return sharedPreferences.edit().putInt(key, value).commit();

    }

    public static int getIntValue(String key) {

        if (sharedPreferences.contains(key))
            return sharedPreferences.getInt(key, DEFAULTINT);

        return DEFAULTINT;

    }


    public static boolean putStringValue(String key, String value) {

        return sharedPreferences.edit().putString(key, value).commit();

    }


    public static String getStringValue(String key) {
        if (sharedPreferences.contains(key))

            return sharedPreferences.getString(key, DEFAULTSTRING);

        return DEFAULTSTRING;


    }


    public static boolean putBooleanValue(String key, boolean value) {

        return sharedPreferences.edit().putBoolean(key, value).commit();

    }

    public static boolean getBooleanValue(String key) {
        if (sharedPreferences.contains(key))

            return sharedPreferences.getBoolean(key, DEFAULTBOOLEAN);

        return DEFAULTBOOLEAN;


    }


    public static boolean putLongValue(String key, long value) {

        return sharedPreferences.edit().putLong(key, value).commit();

    }

    public static long getLongValue(String key) {
        if (sharedPreferences.contains(key))
            return sharedPreferences.getLong(key, DEFAULTLONG);
        return DEFAULTLONG;

    }

    public static boolean putFloatValue(String key, float value) {

        return sharedPreferences.edit().putFloat(key, value).commit();

    }

    public static float getFloatValue(String key) {
        if (sharedPreferences.contains(key))
            return sharedPreferences.getFloat(key, DEFAULTFLOAT);
        return DEFAULTFLOAT;
    }


    public static boolean putSetStringValue(String key, Set<String> value) {

        return sharedPreferences.edit().putStringSet(key, value).commit();

    }


    public static Set<String> getSetStringValue(String key) {
        if (sharedPreferences.contains(key))
            return sharedPreferences.getStringSet(key, null);
        return null;
    }


}
